﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using NativeExcel;
using System.Reflection;
using System.ComponentModel;
using Sinacor.Infra.UI.Common;
using System.Windows;
using PSE.Framework.UI.Resources;
using System.Collections;
using Sinacor.Infra.Common;
using System.Windows.Data;

namespace Sinacor.Infra.UI.Controls.SmartClient.DataGrid
{
    public partial class SinacorDataGrid
    {
        //#region GetPropertyInfo
        ////private List<PropertyValueType> GetPropertyInfo(Object item, List<string> properties)
        ////{
        ////    List<PropertyValueType> propertiesInfo = new List<PropertyValueType>();

        ////    foreach (string property in properties)
        ////    {
        ////        string[] fieldsName = property.Split('.');

        ////        Type propType = item.GetType();
        ////        PropertyInfo propInfo = null;
        ////        object propValue = item;

        ////        foreach (string field in fieldsName)
        ////        {
        ////            propInfo = propType.GetProperty(field);

        ////            if (propInfo != null)
        ////            {
        ////                propType = propInfo.PropertyType;
        ////                propValue = propInfo.GetValue(propValue, null);

        ////                if (propType == null || propValue == null)
        ////                    break;
        ////            }
        ////            else
        ////            {
        ////                propType = null;
        ////                propValue = null;
        ////                break;
        ////            }
        ////        }

        ////        if (propType != null)
        ////        {
        ////            PropertyValueType propertyValueType = new PropertyValueType();
        ////            propertyValueType.PropertyType = propType;
        ////            propertyValueType.PropertyValue = propValue;

        ////            propertiesInfo.Add(propertyValueType);
        ////        }

        ////    }

        ////    return propertiesInfo;
        ////}
        //#endregion

        #region FormatProperties
        private object FormatProperties(object item, BindingBase binding)
        {
            object value = null;
            string format = binding.StringFormat;
            List<string> properties = new List<string>();

            if (binding is Binding)
            {
                if (((Binding)binding).Path != null && !string.IsNullOrEmpty(((Binding)binding).Path.Path))
                    properties.Add(((Binding)binding).Path.Path);
            }
            else if (binding is MultiBinding)
            {
                foreach (Binding b in ((MultiBinding)binding).Bindings)
                {
                    if (b.Path != null && !string.IsNullOrEmpty(b.Path.Path))
                        properties.Add(b.Path.Path);
                }
            }

            if (properties.Count > 0)
            {
                List<object> props = item.GetPropertyInfo(properties).ConvertAll<object>(p => GetPropertyValue(p.PropertyValue));

                //Não possui StringFormat, monta string default
                if (String.IsNullOrEmpty(format))
                {
                    for (int i = 0; i < props.Count; i++)
                    {
                        if (i > 0)
                            format += " ";

                        format += "{" + i + "}";
                    }
                }
                else if (!format.StartsWith("{") && !format.EndsWith("}"))
                {
                    format = "{0:" + format + "}";
                }

                value = string.Format(format, props.ToArray());
            }

            return value;
        }
        #endregion

        #region FillItemsInfo
        /// <summary>
        /// Alimenta coleção que será disponibilizada nos Eventos ExportingExcel e ExportedExcel
        /// </summary>
        /// <returns></returns>
        private List<ItemInfo> FillItemsInfo(IEnumerable items)
        {
            List<ItemInfo> itemsInfo = new List<ItemInfo>();

            foreach (object item in items)
            {
                // Incluindo na coleção
                itemsInfo.Add(new ItemInfo(this.GetItemsSourceList().IndexOf(item), item));
            }

            return itemsInfo;
        }
        #endregion

        #region GetPropertyValue
        private object GetPropertyValue(object value)
        {
            object propertyValue = null;

            //Boleano - Sim ou Não
            if (value is bool ||
                value is bool?)
            {
                if (value != null)
                {
                    if (Convert.ToBoolean(value))
                        propertyValue = (string)ResourceProviderManager.GetResourceProvider().TryGetResource("Infra.Common.Label.Sim");
                    else
                        propertyValue = (string)ResourceProviderManager.GetResourceProvider().TryGetResource("Infra.Common.Label.Nao");
                }
            }
            //EnumWrapper - Retorna Descrição do Enum
            else if (value is IEnumWrapper)
            {
                propertyValue = ((IEnumWrapper)value).Text;
            }
            //Qualquer outro Type - Retorna o mesmo Type
            else
            {
                propertyValue = value;
            }

            return propertyValue;
        }
        #endregion
    }
}
